#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
#include "lcd.h"
#include "bmp.h"
#include "touch.h"
#include "data.h"
#include "font.h"

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        printf("file + ip !!!n");
        return -1;
    }
    s = lcd_init();
    if (s == NULL)
    {
        printf("lcd init fail\n");
    }
    Init_Font(); // 显示字体库前先调用本函数进行初始化
    data_init(); // 读取数据前初始化节点设备
    int ret = socket_init(argv[1]);
    server_ip = argv[1];
    socket_connect();
    pthread_t thread_send, thread_adjust;
    pthread_create(&thread_send, NULL, data_send, NULL);
    pthread_detach(thread_send);
    pthread_create(&thread_read, NULL, data_read, NULL);
    pthread_detach(thread_read);
    pthread_create(&thread_adjust, NULL, data_adjust, NULL);
    pthread_detach(thread_adjust);
    diao *touch_s = malloc(sizeof(*touch_s));
    touch_s->touch_diao_x = 0;
    touch_s->touch_diao_y = 0;
    char buf[][7] = {"/1.bmp", "/2.bmp", "/3.bmp", "/4.bmp", "/5.bmp", "/16.bmp"};
    char buflj[][8] = {"/15.bmp", "/14.bmp"};
    bmp_display(s, 0, 0, buf[0]);
    pthread_t thread_root;
    myindex = 0;
    while (1)
    {
        /* code */
        dan_dian(touch_s);

        if (0 == myindex && (touch_s->touch_diao_x >= 480 && touch_s->touch_diao_y >= 100) && (touch_s->touch_diao_x <= 650 && touch_s->touch_diao_y <= 260))
        { // 数据分析
            myindex = 1;
            network_cmd = 0;
            adjust_cmd = 0;
            ret = pthread_create(&thread_root, NULL, data_myprint, s);
            if (0 != ret)
            {
                perror("pthread_create data_myprint failed");
            }
            pthread_detach(thread_root);
            bmp_display(s, 0, 0, buf[1]);
        }
        else if (0 == myindex && (touch_s->touch_diao_x >= 200 && touch_s->touch_diao_y >= 280) && (touch_s->touch_diao_x <= 370 && touch_s->touch_diao_y <= 460))
        { // 设备控制
            myindex = 2;
            network_cmd = 0;
            adjust_cmd = 0;
            ret = pthread_create(&thread_root, NULL, device_mycontrol, s);
            if (0 != ret)
            {
                perror("pthread_create device_mycontrol failed");
            }
            pthread_detach(thread_root);
        }
        else if (0 == myindex && (touch_s->touch_diao_x >= 730 && touch_s->touch_diao_y >= 330) && (touch_s->touch_diao_x <= 890 && touch_s->touch_diao_y <= 500))
        { // 添加更多
            myindex = 3;
            network_cmd = 0;
            adjust_cmd = 0;
            bmp_display(s, 0, 0, buf[5]);
        }
        else if (0 != myindex &&
                 (touch_s->touch_diao_x >= 0 && touch_s->touch_diao_y >= 0) &&
                 (touch_s->touch_diao_x <= 120 && touch_s->touch_diao_y <= 130))
        { // 返回按钮
            myindex = 0;
            pthread_cancel(thread_root);
            bmp_display(s, 0, 0, buf[0]);
        }
        // 以下都为弹窗控制
        else if (myindex == 0 &&
                 (touch_s->touch_diao_x >= 900 && touch_s->touch_diao_y >= 0) &&
                 (touch_s->touch_diao_x <= 950 && touch_s->touch_diao_y <= 20))
        {
            bmp_display(s, 0, 0, buf[0]);
            if (network_cmd == 0)
            {
                wl_cmd(s);
                adjust_cmd = 0;
            }
            network_cmd = (network_cmd + 1) % 2;
        }
        else if (myindex == 0 &&
                 (touch_s->touch_diao_x >= 980 && touch_s->touch_diao_y >= 0) &&
                 (touch_s->touch_diao_x <= 1035 && touch_s->touch_diao_y <= 20))
        {
            bmp_display(s, 0, 0, buf[0]);
            if (adjust_cmd == 0)
            {
                bz_cmd(s);
                network_cmd = 0;
            }
            adjust_cmd = (adjust_cmd + 1) % 2;
        }
        else if (myindex == 0 &&
                 adjust_cmd == 1 &&
                 (touch_s->touch_diao_x >= 930 && touch_s->touch_diao_y >= 30) &&
                 (touch_s->touch_diao_x <= 1035 && touch_s->touch_diao_y <= 80)) // 智能控制
        {
            if (adjust_control == -1)
            {
                adjust_control = 0;
            }
            else
            {
                adjust_control = -1;
            }
            bz_cmd(s);
        }
        else if (myindex == 0 &&
                 network_cmd == 1 &&
                 (touch_s->touch_diao_x >= 930 && touch_s->touch_diao_y >= 90) &&
                 (touch_s->touch_diao_x <= 1035 && touch_s->touch_diao_y <= 150)) // 网络连接
        {
            if (network_control == -1)
            {
                socket_init(argv[1]);
                socket_connect();
                if (network_control != -1)
                {
                    pthread_create(&thread_read, NULL, data_read, NULL);
                    pthread_detach(thread_read);
                }
            }
            else
            {
                network_control = -1;
                pthread_cancel(thread_read);
                close(fd_socket);
            }
            wl_cmd(s);
            bmp_display(s, 0, 0, buflj[!network_control]);
            sleep(1);
            bmp_display(s, 0, 0, buf[0]);
            wl_cmd(s);
        }
        // printf("x=%d,y=%d\n", touch_s->touch_diao_x, touch_s->touch_diao_y);
    }
    lcd_close(s);
    UnInit_Font(); // 程序退出前，调用本函数
    return 0;
}